CREATE PROCEDURE [dbo].[erp_sp_RefWRRforPI]
	@cCompanyID varchar(10),
	@cCode varchar(20),
	@cCondition varchar(8000)
AS
declare @cmd varchar(8000)

set @cmd = '
SELECT a.cCode,a.cCompanyID, ''WRR'' AS cRepType,a.cWRRNo AS cRefNo,a.cCode AS cRefCode,c.cName AS cRefName,a.cAddress AS cRefAddress,
	a.dDate AS cRefDate,b.cItemNo,b.cDesc,'''' AS cWH,b.cUnit,b.nQty,e.nPrice,e.nAmount,
	b.nIdentity,isnull(d.nAveCost,0) AS nCost,'''' AS cType,b.nFactor,e.nNetPrice,b.cLongDesc
FROM wrr a
	LEFT OUTER JOIN wrr_t b ON a.cWRRNo = b.cWRRNo
	LEFT OUTER JOIN client_supplier c ON c.cCode = a.cCode
	LEFT OUTER JOIN item d ON d.cItemNo = b.cItemNo
	LEFT OUTER JOIN po_t e ON e.cPONo = b.cRefNo
WHERE a.lCancelled=0  AND a.cCode = ''' + @cCode + ''' AND a.cCompanyID = ''' + @cCompanyID + '''' + @cCondition 

PRINT (@cmd)
EXEC (@cmd)
GO


